package com.seqb.utils;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 用于替代写复杂的sql所用，但效率没直接用sql处理高
 * @date 2019/11/11
 */
public class MapUtils {
    /**
     * 把List<Object>转为Map<Integer, Object>，key为item0、item1、item2、item3
     *
     * @param list 待转换的List
     */
    public static Map<String, Object> toItemMap(List<?> list) {
        Map<String, Object> results = Maps.newHashMap();
        if (CollectionUtils.isEmpty(list)) {
            return results;
        }
        for (int i = 0; i < list.size(); i++) {
            results.put("item" + i, list.get(i));
        }
        return results;
    }

    /**
     * 把List<Object>，提取Object中的某个属性值key，转为Map<key, Object>，有重复的key则舍去之前的对象
     *
     * @param list    待转换的List
     * @param keyName List对象中的某个属性名
     */
    public static Map<Object, Object> toMap(List<?> list, String keyName) {
        String keyGetMethod = "get" + keyName.substring(0, 1).toUpperCase() + keyName.substring(1);
        Map<Object, Object> results = Maps.newHashMap();
        try {
            if (CollectionUtils.isNotEmpty(list)) {
                for (Object object : list) {
                    Object key = object.getClass().getMethod(keyGetMethod).invoke(object);
                    results.put(key, object);
                }
            }
        } catch (Exception ignored) {
        }
        return results;
    }

    /**
     * 把List<Object>，提取Object中的某个属性值key,转为Map<key, List<Object>>。不合并重复
     *
     * @param array   待转换的List
     * @param keyName List对象中的某个属性名
     */
    public static Map<String, List<Object>> toMapList(List<?> array, String keyName) {
        String keyGetMethod = "get" + keyName.substring(0, 1).toUpperCase() + keyName.substring(1);
        Map<String, List<Object>> results = Maps.newHashMap();
        try {
            if (CollectionUtils.isNotEmpty(array)) {
                for (Object object : array) {
                    Object invoke = object.getClass().getMethod(keyGetMethod).invoke(object);
                    String key = String.valueOf(invoke);
                    if (key == null) {
                        continue;
                    }
                    List<Object> list = results.get(key);
                    if (list == null) {
                        list = Lists.newArrayList();
                        results.put(key, list);
                    }
                    list.add(object);
                }
            }
        } catch (Exception ignored) {
        }
        return results;
    }

    /**
     * 统计Map<Object, List<Object>>中的List<Object>数量，转为Map<Object, Integer>返回
     */
    public static Map<String, Integer> toMapNum(Map<String, List<Object>> map) {
        HashMap<String, Integer> numMap = Maps.newHashMap();
        for (Map.Entry<String, List<Object>> next : map.entrySet()) {
            numMap.put(next.getKey(), next.getValue().size());
        }
        return numMap;
    }

    /**
     * 统计List<Object>中的某个属性值key的重复数量，转为Map<String, Integer>返回
     * 相当于select count(1) group by keyName
     */
    public static Map<String, Integer> toMapNum(List<?> array, String keyName) {
        String keyGetMethod = "get" + keyName.substring(0, 1).toUpperCase() + keyName.substring(1);
        Map<String, Integer> results = Maps.newHashMap();
        try {
            if (CollectionUtils.isNotEmpty(array)) {
                for (Object object : array) {
                    Object invoke = object.getClass().getMethod(keyGetMethod).invoke(object);
                    String key = String.valueOf(invoke);
                    if (key == null) {
                        continue;
                    }
                    Integer num = results.get(key);
                    if (num == null) {
                        num = 0;
                    }
                    num++;
                    results.put(key, num);
                }
            }
        } catch (Exception ignored) {
        }
        return results;
    }

    /**
     * 统计List<Object>中的某个属性值key的countName总数量，转为Map<String, Integer>返回
     * 相当于select sum(countName) group by keyName
     */
    public static Map<String, Double> toMapNum(List<?> array, String keyName, String countName) {
        String keyGetMethod = "get" + keyName.substring(0, 1).toUpperCase() + keyName.substring(1);
        String countGetMethod = "get" + countName.substring(0, 1).toUpperCase() + countName.substring(1);
        Map<String, Double> results = Maps.newHashMap();
        try {
            if (CollectionUtils.isNotEmpty(array)) {
                for (Object object : array) {
                    Object invoke = object.getClass().getMethod(keyGetMethod).invoke(object);
                    String key = String.valueOf(invoke);
                    if (key == null) {
                        continue;
                    }
                    Double num = results.get(key);
                    if (num == null) {
                        num = 0.0;
                    }
                    Object invoke1 = object.getClass().getMethod(countGetMethod).invoke(object);
                    Double count = Double.parseDouble(String.valueOf(invoke1));
                    num += count;
                    results.put(key, num);
                }
            }
        } catch (Exception ignored) {
        }
        return results;
    }

    /**
     * 将List<Object> 按照Object中的属性值firstKey，secondKey统计数量，最终返回HashMap<firstKey, Map<secondKey, Integer>>。
     * 相当于sql中的select firstKey,secondKey,count(1) from *** group by firstKey,secondKey
     */
    public static HashMap<String, Map<String, Integer>> toMapMapNum(List<?> list, String firstKey, String secondKey) {
        HashMap<String, Map<String, Integer>> mapMapNum = Maps.newHashMap();
        Map<String, List<Object>> mapList = toMapList(list, firstKey);
        for (Map.Entry<String, List<Object>> next : mapList.entrySet()) {
            Map<String, Integer> mapNumMap = MapUtils.toMapNum(next.getValue(), secondKey);
            mapMapNum.put(next.getKey(), mapNumMap);
        }
        return mapMapNum;
    }

    /**
     * 将List<Object> 按照Object中的属性值firstKey，secondKey统计数量，最终返回HashMap<firstKey, Map<secondKey, Integer>>。
     * 相当于sql中的select firstKey,secondKey,sum(countName) from *** group by firstKey,secondKey
     */
    public static HashMap<String, Map<String, Double>> toMapMapNum(List<?> list, String firstKey, String secondKey, String countName) {
        HashMap<String, Map<String, Double>> mapMapNum = Maps.newHashMap();
        Map<String, List<Object>> mapList = toMapList(list, firstKey);
        for (Map.Entry<String, List<Object>> next : mapList.entrySet()) {
            Map<String, Double> mapNumMap = MapUtils.toMapNum(next.getValue(), secondKey, countName);
            mapMapNum.put(next.getKey(), mapNumMap);
        }
        return mapMapNum;
    }

}
